package com.tech.connect.zhaorencai;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.dialog.ItemsDialog;
import com.ksy.common.image.ImagePickActivity;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.EditTextActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.api.JianLiHttp;
import com.tech.connect.api.UserInfo;
import com.tech.connect.model.ItemCatSub;
import com.tech.connect.util.CatUtils;
import com.tech.connect.view.IndexImageLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PublishJianLiActivity extends BaseActivity {

    private IndexImageLayout viewPager;
    private TextView tvName;
    private TextView tvAge;
    private ImageView ivSex;
    private TextView tvAddress;
    private TextView tvSign;
    private TextView tvMoney;
    private TextView tvStudy;
    private TextView tvNote;
    private TextView tvDate;
    private RecyclerView jobRecycler;
    private RecyclerView studyRecycler;
    private List<String> allJobList = new ArrayList<>();
    private List<String> allStudyList = new ArrayList<>();
    private BaseAdapter<String, BaseHolder> jobAdapter;
    private BaseAdapter<String, BaseHolder> studyAdapter;
    private CheckBox checkbox;
    private ItemCatSub selectDateCat;
    private ItemCatSub selectStudyCat;
    private ItemCatSub selectMoneyCat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jianli_edit);
        getHeadBar().setTitle("个人简历");
        getHeadBar().setRightText("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkbox.isChecked()) {
                    showToast("请阅读并同意发布规则");
                    return;

                }
                if (selectMoneyCat == null) {
                    showToast("请选择年薪");
                    return;
                }
                if (selectDateCat == null) {
                    showToast("请选择工作经验");
                    return;
                }
                if (selectStudyCat == null) {
                    showToast("请选择学历");
                    return;
                }
                update();

            }
        });
        initView();
        initData();
        loadData();
        initJobAdapter(null);
        initStudyAdapter(null);
    }

    private void initData() {
        UserInfo user = UserInfo.getUserInfo();
        tvName.setText(user.nickName);
        if (user.gender == 0) {
            ivSex.setImageResource(R.drawable.icon_women);
        } else {
            ivSex.setImageResource(R.drawable.icon_man);
        }
        tvAge.setText(String.valueOf(user.age));
        tvAddress.setText(user.cityName);
        tvSign.setText("签名：" + user.sign);
    }

    private void initView() {
        findViewById(R.id.ivEdit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagePickActivity.comeIn(activity, true, 6, 996);
            }
        });
        findViewById(R.id.tvAddJob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EditJobJingLiActivity.class, 10001);
            }
        });
        findViewById(R.id.tvAddStudy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EditStudyJingLiActivity.class, 10002);
            }
        });
        viewPager = findViewById(R.id.viewPager);
        tvName = findViewById(R.id.tvName);
        ivSex = findViewById(R.id.ivSex);
        tvAge = findViewById(R.id.tvAge);
        tvAddress = findViewById(R.id.tvAddress);
        tvSign = findViewById(R.id.tvSign);

        tvMoney = findViewById(R.id.tvMoney);
        tvStudy = findViewById(R.id.tvStudy);
        tvDate = findViewById(R.id.tvDate);

        jobRecycler = findViewById(R.id.jobRecycler);
        jobRecycler.addItemDecoration(new HLineDivider());
        jobRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        studyRecycler = findViewById(R.id.studyRecycler);
        studyRecycler.addItemDecoration(new HLineDivider());
        studyRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        tvNote = findViewById(R.id.tvNote);
        checkbox = findViewById(R.id.checkbox);

        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EditTextActivity.class, 10003);
            }
        });
        tvMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                final List<ItemCatSub> listByCode = CatUtils.getInstance().getCatListByCode("salary");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, listByCode, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        dialog.dismiss();
                        selectMoneyCat = listByCode.get(position);
                        tvMoney.setText(selectMoneyCat.description);
                    }
                });
                itemsDialog.show();
            }
        });
        tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                final List<ItemCatSub> listByCode = CatUtils.getInstance().getCatListByCode("work");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, listByCode, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        dialog.dismiss();
                        selectDateCat = listByCode.get(position);
                        tvDate.setText(selectDateCat.description);
                    }
                });
                itemsDialog.show();
            }
        });
        tvStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                final List<ItemCatSub> listByCode = CatUtils.getInstance().getCatListByCode("education");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, listByCode, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        dialog.dismiss();
                        selectStudyCat = listByCode.get(position);
                        tvStudy.setText(selectStudyCat.description);
                    }
                });
                itemsDialog.show();
            }
        });

        viewPager.updateImages(null, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (RESULT_OK == resultCode) {
            if (requestCode == 996) {
                ArrayList<String> paths = intent.getStringArrayListExtra(ImagePickActivity.Data_selectImages);
                viewPager.updateImages(paths, 0);
            } else if (requestCode == 10001) {

            } else if (requestCode == 10002) {

            } else if (requestCode == 10003) {

            } else {

            }
        }
    }

    private void loadData() {
        HttpUtils.jianliDetail("1", new BaseEntityOb<Map<String, String>>() {
            @Override
            public void onDataDeal(boolean isSuccess, Map<String, String> data, String msg) {

            }
        });
    }

    private void initJobAdapter(List<String> list) {
        allJobList.clear();
        if (list != null) {
            allJobList.addAll(list);
        }
        if (jobAdapter == null) {
            jobAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_jinli, allJobList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
//                    String s = allJobList.get(position);
//                    TextView tv = helper.getView(R.id.tv);
//                    tv.setText(s);
                }
            };
            jobRecycler.setAdapter(jobAdapter);
        }
        jobAdapter.notifyDataSetChanged();
    }

    private void initStudyAdapter(List<String> list) {
        allStudyList.clear();
        if (list != null) {
            allStudyList.addAll(list);
        }
        if (studyAdapter == null) {
            studyAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_jinli, allStudyList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
//                    String s = allJobList.get(position);
//                    TextView tv = helper.getView(R.id.tv);
//                    tv.setText(s);
                }
            };
            studyRecycler.setAdapter(studyAdapter);
        }
        studyAdapter.notifyDataSetChanged();
    }

    private void update() {
        Map<String, Object> map = JianLiHttp.getMap();
//        map.put("areaCode", UserInfo.getUserInfo().cityCode);
//        map.put("areaCode", "310000");
//        List<String> allList = viewPager.getAllList();
//        String imageParams = ImageApi.getImageParams(allList);
//        map.put("avatar", imageParams);
        map.put("educationId", selectStudyCat.value);
        map.put("educationName", selectStudyCat.description);
        map.put("introduce", tvNote.getText().toString().trim());
        map.put("lat", CurrentInfo.getUserInfo().lat);
        map.put("lng", CurrentInfo.getUserInfo().lng);
        map.put("salaryId", selectMoneyCat.value);
        map.put("salaryName", selectMoneyCat.description);
        map.put("workingId", selectDateCat.value);
        map.put("workingName", selectDateCat.description);
        JianLiHttp.create(map, new BaseEntityOb<String>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, String data, String msg) {
                hideDialog();
                showToast(msg);

            }
        });
    }
}